<?php

class My_Pattern {
	
	/**
	 * Pattern
	 * @var string
	 */
	protected $_pattern = "[:titleBefore][:titleBehind][:firstName][:lastName]";
	
		
	/**
	 * 
	 * @param string $pattern 
	 * @return array
	 */
	function __construct($pattern) {
		
		$this->setPattern($pattern);
		
	}
	
	public function getPattern(){
		return $this->_pattern;
	}
	
	public function setPattern($pattern){
		
		if(!is_string($pattern)){
			throw new My_Pattern_Exception('Pattern must be a string');	
		}
		
		$this->_pattern = $pattern;
	}
	
	/**
	 * Vytvori asociativne pole podla My_Pattern
	 * @param array $data
	 */		
	public function makeArray($data){
		$pattern = str_replace(array('[',']'),'',$this->_pattern);
		$pattern = explode(':',trim($pattern,':'));
		
		if(count($pattern) != count($data)){
			require_once 'library/My/Pattern/Exception.php';
			throw new My_Pattern_Exception('Bad csv file format, count of $data and $pattern must be equal');
		}
		
		$array = array();
		
		for ($i = 0; $i < count($data); $i++) {
			$array[$pattern[$i]] = $data[$i];
		}
		
		return $array;
	}
}